package com.threeti.mecool.core.application.report;

import java.io.Serializable;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Configurable;

import com.threeti.mecool.core.domain.model.basicdata.AdminDivisionLevelDeep;

@Configurable
public class StoreOpeningRateReportCriteria implements Serializable {
	
	private String projectCode;
	private String adCode;
	private int adLevel;
	private String channelSysCatCode;
	private String channelCatCode;
	
	public void setChannelCatCode(String channelCatCode) {
		this.channelCatCode = channelCatCode;
	}
	
	public String getChannelCatCode() {
		return channelCatCode;
	}
	
	public String getProjectCode() {
		return projectCode;
	}

	public void setProjectCode(String projectCode) {
		this.projectCode = projectCode;
	}

	public String getAdCode() {
		return adCode;
	}

	public void setAdCode(String adCode) {
		this.adCode = adCode;
	}

	public int getAdLevel() {
		return adLevel;
	}

	public void setAdLevel(int adLevel) {
		this.adLevel = adLevel;
	}

	public String getChannelSysCatCode() {
		return channelSysCatCode;
	}

	public void setChannelSysCatCode(String channelSysCatCode) {
		this.channelSysCatCode = channelSysCatCode;
	}

	public String toQueryConditionSQL(String preSql) {
		
		
		if (StringUtils.isNotBlank(this.getProjectCode())) {
			preSql = preSql.replaceAll("\\u003F5"," AND PJ.CODE = '" + this.getProjectCode() + "'");
			preSql = preSql.replaceAll("\\u003F6", " AND JR.PROJECT_CODE = '"+ this.getProjectCode() + "'");
		} else {
			preSql = preSql.replaceAll("\\u003F5", "");
			preSql = preSql.replaceAll("\\u003F6", "");
		}
		
		
		
		if (StringUtils.isNotBlank(this.getAdCode())) {
			int adLevel = this.getAdLevel();
			if (adLevel == AdminDivisionLevelDeep.PROVINCE.ordinal()) {
				preSql = preSql.replaceAll("\\u003F7"," AND CS.CHANNEL_PROVINCE_AD_CODE = '"+ this.getAdCode() + "' ");
				preSql = preSql.replaceAll("\\u003F8"," AND CS.CHANNEL_PROVINCE_AD_CODE = '"+ this.getAdCode() + "' ");
			} else if (adLevel == AdminDivisionLevelDeep.CITY.ordinal()) {
				preSql = preSql.replaceAll("\\u003F7"," AND CS.CHANNEL_CITY_AD_CODE = '" + this.getAdCode()+ "' ");
				preSql = preSql.replaceAll("\\u003F8"," AND CS.CHANNEL_CITY_AD_CODE = '" + this.getAdCode()+ "' ");
			} else if (adLevel == AdminDivisionLevelDeep.DISTRICT_SUBURB.ordinal()) {
				preSql = preSql.replaceAll("\\u003F7", " AND CS.CHANNEL_AD_CODE = '"+ this.getAdCode() + "' ");
				preSql = preSql.replaceAll("\\u003F8", " AND CS.CHANNEL_AD_CODE = '"+ this.getAdCode() + "' ");
			}
		} else {
			preSql = preSql.replaceAll("\\u003F7", "");
			preSql = preSql.replaceAll("\\u003F8", "");
		}
		
		
		
		if (StringUtils.isNotBlank(this.getChannelSysCatCode())) {
			preSql = preSql.replaceAll("\\u003F9"," AND CS.CHANNEL_SYS_CAT in ('" + this.getChannelSysCatCode()+ "')");
			preSql = preSql.replaceAll("\\u003F0"," AND CS.CHANNEL_SYS_CAT in ('" + this.getChannelSysCatCode()+ "')");
		} else {
			if(StringUtils.isNotBlank(this.getChannelCatCode())){
				preSql = preSql.replaceAll("\\u003F9"," AND CS.CHANNEL_SYS_CAT in (select t.pos_sys_type_code from MD_POS_SYS_TYPE t where t.pos_kind_code='"+getChannelCatCode()+"')");
				preSql = preSql.replaceAll("\\u003F0"," AND CS.CHANNEL_SYS_CAT in (select t.pos_sys_type_code from MD_POS_SYS_TYPE t where t.pos_kind_code='"+getChannelCatCode()+"')");
				
			}else{
				preSql = preSql.replaceAll("\\u003F9", "");
				preSql = preSql.replaceAll("\\u003F0", "");
			}
		}
		
		

		return preSql;
	}

	public static StoreOpeningRateReportCriteria newCriteria(Map<String, String> parameterMap) {
		
		StoreOpeningRateReportCriteria criteria=new StoreOpeningRateReportCriteria();
		
		if(StringUtils.isNotBlank(parameterMap.get("sysCategory"))){
			criteria.setChannelSysCatCode(parameterMap.get("sysCategory"));
		}
		
		if(StringUtils.isNotBlank(parameterMap.get("channelCatCode"))){
			criteria.setChannelCatCode(parameterMap.get("channelCatCode"));
		}
		
		if(StringUtils.isNotBlank(parameterMap.get("projectCode"))){
			criteria.setProjectCode(parameterMap.get("projectCode"));
		}
		
		return criteria;
	}

}
